package android.bkav.testing.activity;

import java.util.Date;

import android.app.Activity;
import android.app.NotificationManager;
import android.bkav.testing.R;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.bkav.testing.utils.*;

public class CreateNewNoteActivity extends Activity {
	
	private EditText edtTitleNote;
	private EditText edtNoteContent;
	private DatabaseNoteAdapter dbHelper;
	private NotificationManager mNotificationManager;
	private int NOTE_NOTIFICATION_ID;
	private NewNoteNotification object;
	
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.view_edit_create_a_note_screen);
		
		mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
		object = new NewNoteNotification(this);
		
		edtTitleNote = (EditText)findViewById(R.id.edtNote_Title);
		edtNoteContent = (EditText)findViewById(R.id.editNoteContent);
		
		dbHelper = new DatabaseNoteAdapter(this);
		dbHelper.open();
		
		Button btnSave = (Button) findViewById(R.id.btnSave);
		btnSave.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String title = edtTitleNote.getText().toString();
				String content = edtNoteContent.getText().toString();
				long date_time = new Date().getTime();
				if(checkDataInput(title, content)){
					if(dbHelper.createDbNotes(title, content, date_time) > 0){
						Toast.makeText(getBaseContext(), "Add note success!", Toast.LENGTH_LONG).show();
						
						mNotificationManager.notify(NOTE_NOTIFICATION_ID ++, object.createNewNoteNotification(title, content, System.currentTimeMillis(), CreateNewNoteActivity.class));
						restoreDataInput();
					} else{
						Toast.makeText(getBaseContext(), "Error", Toast.LENGTH_LONG);
					}
				}else
					Toast.makeText(getBaseContext(), "Enter Note Content", Toast.LENGTH_LONG).show();
			}
		});
		
	}
	
	public boolean checkDataInput(String title, String content){
		if(content.equals("")){
			return false;
		}else
			return true;
	}
	
	public void restoreDataInput(){
		edtNoteContent.setText("");
		edtTitleNote.setText("");
	}
	
}
